<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 封装 findeIndex 函数返回查找元素在数组中的索引号。
        function findeIndex(arr,val){
            if (!Array.isArray(arr) || arr.length === 0) {
                return -1;
            }
            let i = 0;
            while(i < arr.length){
                if(val === arr[i]){
                    return i;
                }
                i++;
            }
            return -1;
        }
        const fruits = ['苹果', '香蕉', '橘子', '荔枝', '梨子'];
    const searchItem = '香蕉';
    const index = findeIndex(searchItem, fruits);

    console.log(`查找 "${searchItem}" 在数组中的索引是: ${index}`);

    </script>
    
</body>
</html>